using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class SkillEditor : MonoBehaviour
{
    public SkillListPanel List;
    public SkillEditorPanel Editor;
    List<SkillObject> data = new List<SkillObject>();
    SkillObject selectBuff;
    public string CurrentSkillPath{get;set;}

    void Awake()
    {
        List.Refresh.onClick.AddListener(() => {
            // List.RefreshList();
            // Debug.Log("asdfsadf");
            refreshList();
        });
        // List.Hiden.onClick.AddListener(() => {
        //     List.ShowOrHiden();
        // });
    }

    void refreshList()
    {
        searchSkill();
        List.CSR.UpdateList();
    }

    //search skill dictionary
    void searchSkill()
    {
        data.Clear();
        //search dictionary with assetdatabase
        string path = "Assets/Resources/Skill";
        string[] guids = AssetDatabase.FindAssets("t:SkillObject", new string[] { path });
        foreach (var guid in guids)
        {
            string assetPath = AssetDatabase.GUIDToAssetPath(guid);
            SkillObject conf = AssetDatabase.LoadAssetAtPath<SkillObject>(assetPath);
            Debug.Log(conf);
            Debug.Log(conf.Id + "-" + conf.name);
            data.Add(conf);
        }
        Debug.Log("data.Count:" + data.Count);
        // sort data id 
        data.Sort((a, b) => {
            return a.Id.CompareTo(b.Id);
        });
    }
    private void UpdateItem(GameObject item, int index)
    {
        string name = data[index].Id + "-" + data[index].name;
        var skillitem = item.GetComponent<SkillEditorItem>();
        skillitem.index = index;
        skillitem.Text.text = name;
        skillitem.onClickCallback = OnClickItem;
    }

    public void OnClickItem(int index)
    {
        // Debug.Log("OnClickItem index:" + index);
        Debug.Log("OnClickItem data[index]:" + data[index].Id + "-" + data[index].name);
        // Debug.Log(selectBuff == data[index]);
        if(selectBuff == data[index]) return;
        selectBuff = data[index];
        Editor.ShowBuff(selectBuff);
    }

    public void NewSkill()
    {
#if UNITY_EDITOR
        var assetName = "NewSkill";
        var i = 0;
        while(true)
        {
            var newAssetName = assetName;
            if(i > 0)
            {
                newAssetName += i;
            }

            var asset = AssetDatabase.LoadAssetAtPath<SkillObject>("Assets/Resources/Skill/" + newAssetName + ".asset");
            if(asset == null)
            {
                assetName = newAssetName;
                break;
            }

            i++;
        }

        CurrentSkillPath = "Assets/Resources/Skill/" + assetName + ".asset";
        var skill = ScriptableObject.CreateInstance<SkillObject>();
        skill.Id = i.ToString();
        skill.TotalTime = 2f;
        AssetDatabase.CreateAsset(skill, CurrentSkillPath);
        refreshList();
        Editor.ShowBuff(skill);
#endif
    }

    // Start is called before the first frame update
    void Start()
    {
        List.CSR.Init(UpdateItem);
        searchSkill();
        List.CSR.ShowAndUpdateList(data.Count);
        // refreshList();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
